package com.squirrel.michale;

import java.util.Locale;
import java.util.regex.Pattern;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/3 7:47 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode2288 {

    public String discountPrices(String sentence, int discount) {
        String regularEx = "^\\$[1-9]{1}[0-9]{0,9}";
        Pattern regular = Pattern.compile(regularEx);
        String[] words = sentence.split(" ");
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            StringBuffer discountBuffer = new StringBuffer();
            if (regular.matcher(word).matches()) {
                Double priceDouble = Double.valueOf(word.substring(1))* (100-discount)/ 100;
                String discountPrice = String.format(Locale.CHINA, "%.2f", priceDouble);
                discountBuffer.append("$").append(discountPrice);
                words[i] = discountBuffer.toString();
            }
            result.append(words[i]).append(" ");
        }
        return result.substring(0, result.length()-1).toString();
    }

    public static void main(String[] args) {
        LeetCode2288 leetCode2288 = new LeetCode2288();

        String s = "there are $1 $2 and 5$ candies in the shop";
        int dis = 50;
        System.out.println(leetCode2288.discountPrices(s, dis));

    }
}
